#include "myserialport.h"

MySerialPortSettings::MySerialPortSettings()
{
}

MySerialPortSettings::MySerialPortSettings(const MySerialPortSettings &setting)
{
    portName = setting.portName;
    baudRate = setting.baudRate;
    parity = setting.parity;
    dataBits = setting.dataBits;
    stopBits = setting.stopBits;
    flowControl = setting.flowControl;
}

/// @brief = 运算符重载
/// @param setting
/// @return this
MySerialPortSettings &MySerialPortSettings::operator=(const MySerialPortSettings &setting)
{
    if (this != &setting)
    {
        portName = setting.portName;
        baudRate = setting.baudRate;
        parity = setting.parity;
        dataBits = setting.dataBits;
        stopBits = setting.stopBits;
        flowControl = setting.flowControl;
    }
    return *this;
}

MySerialPort::MySerialPort(const MySerialPortSettings &setting, RES &res, QObject *parent)
    : QObject(parent), _thread(nullptr), _sp(nullptr)
{
    _sp = new QSerialPort();
    int returnCode = init(setting);

    if (returnCode < 0)
    {
        res.returnCode = returnCode;
        res.msg = _sp->errorString();
        return;
    }

    //_receTimer.moveToThread(_thread);

    // connect(&_receTimer,&QTimer::timeout,this,&MySerialPort::slReceiveTimeOut);
}

MySerialPort::~MySerialPort()
{
    if (_thread)
    {
        _thread->quit();
        _thread->wait();
        _thread->deleteLater();
    }

    if (_sp)
    {
        _sp->deleteLater();
    }

    qDebug() << "MySerialPort Destroyed!";
}

/// @brief 初始化我的串口（任务对象） 开启串口线程（moveToThread）
/// @param const MySerialPortSettings &setting
/// @return 异常返回-1 正常返回0
int MySerialPort::init(const MySerialPortSettings &setting)
{
    // 初始化我的串口
    _sp->setPortName(setting.portName);
    _sp->setBaudRate(setting.baudRate);
    _sp->setDataBits(setting.dataBits);
    _sp->setParity(setting.parity);
    _sp->setStopBits(setting.stopBits);
    _sp->setFlowControl(setting.flowControl);
    //_sp->setReadBufferSize(1024);

    if (_sp->open(QIODevice::ReadWrite)) // 正常打开
    {
        qDebug() << "MySerialPort opened successfully!";

        // 创建线程
        _thread = new QThread();
        _sp->moveToThread(_thread);
        this->moveToThread(_thread);
        _thread->start();

        // 连接 readyRead信号 与 串口接收槽函数
        connect(_sp, &QSerialPort::readyRead, this, &MySerialPort::slReceiveData, Qt::QueuedConnection);
    }
    else // 打开失败
    {
        qDebug() << "Failed to open MySerialPort:" << _sp->errorString();
        return -1;
    }

    return 0;
}

/// @brief 对QSerialPort类的 readReady信号 对接的 槽函数
void MySerialPort::slReceiveData()
{
    // if(!_receTimer.isActive()) _receTimer.start(30);
    // QMutexLocker locker(&_mutex);
    // _receBytes.append(_sp->readAll());

    emit sgSPReceiveData(_sp->readAll());
    // emit sgSPReceiveData(dk,_receiveDataMap[dk].data);
}

/// @brief 发送串口信息 槽函数
/// @param data
void MySerialPort::slSendData(QString data)
{
    int res = _sp->write(data.toLatin1());

    if (res < 0) // 异常
    {
        qDebug() << "bad write";
    }
}

void MySerialPort::slTimerRegularSend(bool checked)
{
//    if(checked == true)
//    {
//        timerRegular->start();
//    }
//    else
//    {
//        timerRegular->stop();
//    }
}
